package kr.hkit.androidcomplete03.ch25;

import kr.hkit.androidcomplete03.R;
import android.app.IntentService;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class NewsService extends IntentService {
	private boolean mQuit;

	public NewsService() {
		super("NewsService");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		String[] arNews = getResources().getStringArray(R.array.news);
		for (int idx = 0; mQuit == false; idx++) {
			Message msg = new Message();
			msg.what = 0;
			msg.obj = arNews[idx % arNews.length];
			mHandler.sendMessage(msg);

			try {
				Thread.sleep(5000);
			} catch (Exception e) {
				;
			}
		}

	}

	public void onDestroy() {
		super.onDestroy();
		mQuit = true;
	}

	public int onStartCommand(Intent intent, int flags, int startId) {
		mQuit = false;
		return super.onStartCommand(intent, flags, startId);
	}

	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 0) {
				String news = (String) msg.obj;
				Toast.makeText(NewsService.this, news, 0).show();
			}
		}
	};

}
